package com.kunbo.cn.service.impl.core.logistics;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.kunbo.cn.dao.core.logistics.TrafficUnitDao;
import com.kunbo.cn.entity.core.logistics.TrafficUnit;
import com.kunbo.cn.service.core.logistics.TrafficUnitService;

@Service
public class TrafficUnitImpl implements TrafficUnitService{
	
	@Autowired
	private TrafficUnitDao dao;
	@Autowired
	private JdbcTemplate jdbc;
	
	@Override
	public TrafficUnit save(String affiliate, String unit_name, String phone, String address,String unit_number) {
		// TODO Auto-generated method stub
		TrafficUnit  unit = new TrafficUnit();
		String supplier = "select name from business_supplier_manage where id = " + Integer.parseInt(affiliate);
		Map<String,Object> map = jdbc.queryForMap(supplier);
		if(map.size() > 0 ) {
		unit.setAddMechanism(map.get("name").toString());
		}
		unit.setNameOfTransportUnit(unit_name);
		unit.setContactNumber(phone);
		unit.setCompanyAddress(address);
		unit.setIsItPermitted("false");
		unit.setTransportUnitNumber(unit_number);
		unit = dao.saveAndFlush(unit);
		if(unit != null)
			return unit;
		else
			return null;
	}
	@Override
	public List<Map<String, Object>> selTrafficUnit(String affiliate,Integer page,Integer pageSize) {
		// TODO Auto-generated method stub
		String supplier = "select name from business_supplier_manage where id = " + Integer.parseInt(affiliate);
		Map<String,Object> map = jdbc.queryForMap(supplier);
		if(map.size() > 0 ) {
			String sql = "select id, name_of_transport_unit,contact_number,company_address from transport_unit_management where add_mechanism = '" +map.get("name").toString()+"' order by id desc limit " + (page*pageSize) +"," + pageSize;
			return jdbc.queryForList(sql);
		}
		return null;
	}


	@Override
	public Boolean delTrafficUnit(String id) {
		// TODO Auto-generated method stub
		dao.delete(id);
		TrafficUnit unit = dao.findOne(id);
		if(unit == null)
			return true;
		else 
			return false;
	}

}
